<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:a="https://github.com/pylonide/pylon" skipParse="[%packed%]">
    <head profile="http://www.w3.org/2005/10/profile">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>Pylon - [%projectName%]</title>
        <meta name="description" content=""/>
        <meta name="keywords" content=""/>
        <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, user-scalable=no"/>
          <meta name="apple-mobile-web-app-capable" content="yes"/>
          <meta name="apple-mobile-web-app-status-bar-style" content="black"/>
          <meta name="format-detection" content="telephone=no"/>
        <script type="text/javascript">
        //<![CDATA[
            if (<%local%>)
                document.title += " Local";
        //]]>
        </script>
        <link rel="icon" type="image/gif" href="[%staticUrl%]/favicon.ico" />

        <link rel="stylesheet" type="text/css" href="[%staticUrl%]/ext/main/style/loading.css" />

        <script type="text/javascript">
            function createSplash() {
                var msg = (function () {
                    var msgs = [
                        "It will be just as you left it"
                    ];
                    return msgs[Math.random() * msgs.length | 0];
                }());
                    
                var loadingIde = document.createElement("div");
                loadingIde.setAttribute("id", "loadingide");
                
                var header = document.createElement("div");
                header.className = "header";
                var footer = document.createElement("div");
                footer.className = "footer";
                // yes this is bad
                footer.innerHTML = '<a href="https://github.com/pylonide/pylon/wiki">Documentation</a> | <a href="https://github.com/exsilium/cloud9/issues">Support</a>';
                
                var content = document.createElement("div");
                content.className = "loading-progress";
                content.innerHTML = '<div id="progress-msg">Preparing your workspace <span id="dots"></div></div><div class="loading-seperator"></div><div class="loading-msg"></div>';
                content.querySelector(".loading-msg").textContent = msg;

                loadingIde.appendChild(header);
                loadingIde.appendChild(content);
                loadingIde.appendChild(footer);
                document.documentElement.appendChild(loadingIde);
                
                var dots = document.getElementById("dots");
                var cnt  = 0;
                window.dotsAnim = setInterval(function () {
                    dots.textContent = "...".split("").slice(0, (++cnt)%4).join("");
                }, 500);
            }
            function clearSplash () {
                // stop the dots animation
                clearInterval(window.dotsAnim);
                
                var loadingDiv = document.getElementById("loadingide");
                if (loadingDiv)
                    document.documentElement.removeChild(loadingDiv);
            }
            createSplash();
        </script>

        <link rel="stylesheet" type="text/css" href="[%staticUrl%]/ext/main/style/style.css" />

        [%loadedDetectionScript%]
        <script type="text/javascript" src="[%staticUrl%]/require.js"></script>
        [%scripts%]
        <script type="text/javascript">
        //<![CDATA[

            window.cloud9config = {
                version: <%version%>,
                startdate: new Date(),
                davPrefix: <%davPrefix%>,
                workspaceDir: <%workspaceDir%>,
                debug: <%debug%>,
                sessionId: <%sessionId%>,
                workspaceId: <%workspaceId%>,
                runners: <%runners%>,
                name: <%name%>,
                uid: <%uid%>,
                pid: <%pid%>,
                readonly: <%readonly%>,
                projectName: <%projectName%>,
                version: <%version%>,
                workerUrl: "[%workerUrl%]",
                staticUrl: "[%staticUrl%]",
                smithIo: <%smithIo%>,
                hosted: [%hosted%],
                local: <%local%>,
                env: <%env%>,
                settings: <%settingsXml%>,
                packed: <%packed%>,
                packedName: <%packedName%>,
                _csrf: <%_csrf%>
            };

            window.onerror = function(m, u, l) {
                if (m.match(/Load timeout for modules:/) || m.match(/Script error/))
                    return;
                clearSplash();
            };

            // prevent console messages from crashing our app!
            if (typeof window["console"] == "undefined") {
                var K = function() {};
                window.console = {log:K,debug:K,dir:K,trace:K,error:K,warn:K,profileStart:K,profileEnd:K};
            }

            if (!cloud9config.packed) {
                var RELEASE = "";
                var DEBUG   = "release";
                var FILES   = "files";

                var VERSION = window.cloud9config.debug ? DEBUG : RELEASE;

                var ppcLoc = "[%staticUrl%]/ppc/ppc" + VERSION + ".js";
            }
            else {
               var ppcLoc = "";
            }
            
            var config = <%requirejsConfig%>; 

            // configure text plugin to use XHR for crossorigin requests instead of falling back to .js files
            config.config = {text: {useXhr: function(){return true;}}};

            require(config, [ppcLoc], function(){

                if (!cloud9config.packed) {
                    main();
                }
                else {
                    var packedFile = "[%staticUrl%]/" + (cloud9config.packedName || "c9os.min.js");
                    require([packedFile], main);
                }
                
                preloaderImgs();
            });
            
            function main() {
                var list = ["core/ide", "core/ext", "core/util", 
                "core/settings", "ext/main/main", "ext/editors/editors",
                "ace/editor"];
                require(list, function(ide, ext, util){
                    var plugins = <%plugins%>;

                    // if you pass `force=true` into the querystring, then
                    // we bypass the browser check
                    if (window.cloud9config.hosted && !window.location.search.match(/\bforce\=true\b/)) {
                        // otherwise, ask ppc
                        if (ppc.isIE) {
                            // redirect to the notsupported page
                            var ref = encodeURIComponent(window.location.pathname + window.location.search);
                            window.location.href = "/site/notsupported.html?ref=" + ref;
                        }
                    }

                    var aceConfig = require("ace/config");

                    aceConfig.set("packaged", true);
                    aceConfig.set("workerPath", "[%workerUrl%]/ace/worker");
                    aceConfig.set("modePath", "[%staticUrl%]/ace/mode");
                    aceConfig.set("themePath", "[%staticUrl%]/ace/theme");
                    aceConfig.set("basePath", "[%staticUrl%]/ace/build");

                    //Load extensions
                    ppc.addEventListener("load", function(){
                        require(plugins, function() {
                            loadAndBegin(ide, plugins);
                        });
                    });
                });
            }

            function loadAndBegin(ide, plugins) {
                ide.dispatchEvent("extload", {modules: plugins});

                cloud9config.totalTime = new Date() - cloud9config.startdate;
                console.log("Total Load Time " 
                    + (cloud9config.totalTime) + "ms");

                clearSplash();

                ide.addEventListener("$event.extload", function(cb){
                    cb();
                });
            }

            function preloaderImgs() {
                var preloadImgs = [
                    "[%staticUrl%]/ext/main/style/images/bg-overlay.png",
                    "[%staticUrl%]/ext/main/style/images/cover-heaeder.png",
                    "[%staticUrl%]/ext/main/style/images/cover-logo.png",
                    "[%staticUrl%]/ext/main/style/images/panelBg.png",
                    "[%staticUrl%]/ext/main/style/images/editor_tab.png",
                    "[%staticUrl%]/ext/main/style/images/cloud9logo_transparent.png"
                ];
                if (document.images) {
                    var img;
                    for(var i = 0; i < preloadImgs.length; i++) {
                        img = new Image();
                        img.src = preloadImgs[i];
                    }
                }
            }
        //]]>
        </script>
    </head>
    <body>
    </body>
</html>
